package com.example.cathouse;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cathouse.databinding.CatFragmentBinding;

import java.util.List;

public class CatFragment extends Fragment {
    RecyclerView recyclerView;
    MyAdapter myAdapter;
    CatViewModel catViewModel;
    private LiveData<List<Cat>> filteredCats;

    public static CatFragment newInstance() {
        return new CatFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        setHasOptionsMenu(true);    //显示工具栏

        catViewModel = new ViewModelProvider(requireActivity()).get(CatViewModel.class);
        CatFragmentBinding binding;
        binding = DataBindingUtil.inflate(inflater, R.layout.cat_fragment, container, false);
        binding.setData(catViewModel);
        binding.setLifecycleOwner(requireActivity());
        recyclerView = binding.recyclerView;
        myAdapter = new MyAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        recyclerView.setAdapter(myAdapter);
        filteredCats = catViewModel.getAllCatsLive();
        filteredCats.observe(requireActivity(), new Observer<List<Cat>>() {
            @Override
            public void onChanged(List<Cat> cats) {
                int temp = myAdapter.getItemCount();
                myAdapter.setAllCats(cats);
                if (temp != cats.size()) {
                    myAdapter.notifyDataSetChanged();
                }
            }
        });


        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.main_menu, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
        searchView.setMaxWidth(700);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                String pattern = newText.trim();
                filteredCats.removeObservers(requireActivity());
                filteredCats = catViewModel.findCatsWithPattern(pattern);
                filteredCats.observe(requireActivity(), new Observer<List<Cat>>() {
                    @Override
                    public void onChanged(List<Cat> cats) {
                        int temp = myAdapter.getItemCount();
                        myAdapter.setAllCats(cats);
                        if (temp != cats.size()) {
                            myAdapter.notifyDataSetChanged();
                        }
                    }
                });
                return false;
            }
        });
    }
}